package promote.brozen.algorithm.tree.binary.rbt;

import lombok.Getter;
import lombok.Setter;
import promote.brozen.algorithm.tree.binary.BinaryTreeNode;

import static promote.brozen.algorithm.tree.binary.rbt.RBTTreeNode.Color.RED;

/**
 * @author Brozen
 * @date 2020/1/2 5:29 PM
 */
@Getter
@Setter
public class RBTTreeNode<K extends Comparable<K>> extends BinaryTreeNode<K, RBTTreeNode<K>> {

    private byte color;

    private int height;

    private int blackHeight;

    public RBTTreeNode(K key) {
        this(key, RED);
    }

    public RBTTreeNode(K key, byte color) {
        super(key);
        this.color = color;
        this.height = 0;
        this.blackHeight = 0;
    }

    @Override
    public void setLeftChild(RBTTreeNode<K> left) {
        children.set(0, left);
    }

    @Override
    public void setRightChild(RBTTreeNode<K> right) {
        children.set(1, right);
    }

    @Override
    public RBTTreeNode<K> getParent() {
        return (RBTTreeNode<K>) super.getParent();
    }

    public boolean isBlack() {
        return this.color == Color.BLACK;
    }

    @Override
    public String toString() {
        RBTTreeNode<K> left = getLeftChild();
        RBTTreeNode<K> right = getRightChild();
        return "RBTTreeNode[" + key
                + "] Left[" + (left == null ? null : left.getKey())
                + "] Right[" + (right == null ? null : right.getKey())
                + "] Color[" + (color == RED ? "RED" : "BLACK") + "]" ;
    }

    public interface Color {
        byte RED = 0;
        byte BLACK = 1;
    }

}
